<template>
  <div class="app-container" style="padding: 5px;">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
          <span style="font-weight: bold;color: #0099ff;font-size: 16px;">筛选条件</span>
      </div>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="客户名称" prop="cus">
              <el-input
              v-model="queryParams.cus"
              placeholder="请输入客户名称"
              clearable
              @keyup.enter.native="handleQuery"
              />
          </el-form-item>
          <!-- <el-form-item label="销售人员" prop="saleman">
              <el-input
              v-model="queryParams.saleman"
              placeholder="请输入销售人员"
              clearable
              @keyup.enter.native="handleQuery"
              />
          </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="box-card" style="margin-top: 5px;">
      <div slot="header" class="clearfix">
        <span style="font-weight: bold;color: #0099ff;font-size: 16px;">结果列表</span>
      </div>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-download" size="mini" @click="handleExport" >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="SysTsWorkOrderBiList" border stripe>
        <el-table-column label="周" align="center" prop="wkYear" width="250" />
        <el-table-column label="投诉产品" align="center" prop="tscp" />
        <el-table-column label="问题分类" align="center" prop="wtfl" />
        <el-table-column label="投诉渠道" align="center" prop="tsqd" />
        <el-table-column label="客户姓名" align="center" prop="cus" />
        <el-table-column label="投诉问题" align="left" prop="tswt" width="600" />
        <el-table-column label="处理结果" align="center" prop="cljg" />
        <el-table-column label="操作" align="center" >
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-document" @click="handleView(scope.row)">详情</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" :disabled="scope.row.id==null">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    </el-card>

    <!-- 添加或修改SysTsWorkOrderBi对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-card style="margin-top: -30px;">
        <el-form ref="form" :model="form" :rules="rules" label-width="110px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="年份" prop="year">
                <el-date-picker v-model="form.year" type="year" format="yyyy" value-format="yyyy" style="width:100%;" placeholder="选择年"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="周" prop="wkYear">
                <el-date-picker v-model="form.wkYear" type="week" format="第W周" value-format="yyyy-MM-dd" style="width: 100%;" placeholder="选择周"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="问题分类" prop="wtfl">
                <el-select v-model="form.wtfl" style="width: 100%;" placeholder="请选择">
                  <el-option v-for="item in wtfls" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="投诉渠道" prop="tsqd">
                <el-select v-model="form.tsqd" style="width: 100%;" placeholder="请选择">
                  <el-option v-for="item in tsqds" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="客户姓名" prop="cus">
                <el-input v-model="form.cus" placeholder="请输入客户姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="销售人员" prop="saleman">
                <el-input v-model="form.saleman" placeholder="请输入销售人员" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="投诉产品" prop="tscp">
                <el-input v-model="form.tscp" placeholder="请输入投诉产品" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="归属片区" prop="area">
                <el-input v-model="form.area" placeholder="请输入归属片区" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="投诉问题" prop="tswt">
            <el-input v-model="form.tswt" type="textarea" rows="3" placeholder="请输入投诉问题" />
          </el-form-item>
          <el-form-item label="处理结果" prop="cljg">
            <el-input v-model="form.cljg" placeholder="请输入处理结果" />
          </el-form-item>
          <el-form-item label="其他备注" prop="remark">
            <el-input v-model="form.remark" rows="3" type="textarea" placeholder="请输入其他备注" />
          </el-form-item>
          <el-form-item label="投诉附件" prop="attach">
            <image-upload v-model="form.attach"></image-upload>
          </el-form-item>
        </el-form>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="title1" :visible.sync="open1" width="800px" append-to-body>
      <el-card style="margin-top: -30px;">
        <el-form ref="form" :model="form1" label-width="110px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="年份" prop="year">
                <el-date-picker v-model="form1.year" type="year" format="yyyy" value-format="yyyy" style="width:100%;" placeholder="选择年"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="周" prop="wkYear">
                <el-date-picker v-model="form1.wkYear" type="week" format="第W周" value-format="yyyy-MM-dd" style="width: 100%;" placeholder="选择周"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="问题分类" prop="wtfl">
                <el-select v-model="form1.wtfl" style="width: 100%;" placeholder="请选择">
                  <el-option v-for="item in wtfls" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="投诉渠道" prop="tsqd">
                <el-select v-model="form1.tsqd" style="width: 100%;" placeholder="请选择">
                  <el-option v-for="item in tsqds" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="客户姓名" prop="cus">
                <el-input v-model="form1.cus" placeholder="请输入客户姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="销售人员" prop="saleman">
                <el-input v-model="form1.saleman" placeholder="请输入销售人员" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="投诉产品" prop="tscp">
                <el-input v-model="form1.tscp" placeholder="请输入投诉产品" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="归属片区" prop="area">
                <el-input v-model="form1.area" placeholder="请输入归属片区" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="投诉问题" prop="tswt">
            <el-input v-model="form1.tswt" type="textarea" rows="3" placeholder="请输入投诉问题" />
          </el-form-item>
          <el-form-item label="处理结果" prop="cljg">
            <el-input v-model="form1.cljg" placeholder="请输入处理结果" />
          </el-form-item>
          <el-form-item label="其他备注" prop="remark">
            <el-input v-model="form1.remark" rows="3" type="textarea" placeholder="请输入其他备注" />
          </el-form-item>
          <el-form-item label="投诉附件" prop="attach">
            <image-upload v-model="form1.attach"></image-upload>
          </el-form-item>
        </el-form>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
import { listSysTsWorkOrderBi, getSysTsWorkOrderBi, delSysTsWorkOrderBi, addSysTsWorkOrderBi, updateSysTsWorkOrderBi } from "@/api/system/xssh/khts";

export default {
  name: "SysTsWorkOrderBi",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // SysTsWorkOrderBi表格数据
      SysTsWorkOrderBiList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title1: "",
      // 是否显示弹出层
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        year: null,
        wkYear: null,
        wtfl: null,
        tsqd: null,
        cus: null,
        cphone: null,
        saleman: null,
        sphone: null,
        tscp: null,
        tswt: null,
        cljg: null,
        attach: null,
      },
      // 表单参数
      form: {},
      // 表单参数
      form1: {},
      // 表单校验
      rules: {
        year: [
          { required: true, message: "年份不能为空", trigger: "change" }
        ],
        wkYear: [
          { required: true, message: "周不能为空", trigger: "change" }
        ],
        wtfl: [
          { required: true, message: "问题分类不能为空", trigger: "change" }
        ],
        tsqd: [
          { required: true, message: "投诉渠道不能为空", trigger: "change" }
        ],
        cus: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        saleman: [
          { required: true, message: "销售人员不能为空", trigger: "blur" }
        ],
        tscp: [
          { required: true, message: "投诉产品不能为空", trigger: "blur" }
        ],
        tswt: [
          { required: true, message: "投诉问题不能为空", trigger: "blur" }
        ],
        cljg: [
          { required: true, message: "处理结果不能为空", trigger: "blur" }
        ],
        area: [
          { required: true, message: "归属片区不能为空", trigger: "blur" }
        ],
      },
      wtfls: [{
          value: '死苗',
          label: '死苗'
        }, {
          value: '延期交付',
          label: '延期交付'
        }, {
          value: '服务',
          label: '服务'
        }, {
          value: '货不对版',
          label: '货不对版'
        }, {
          value: '不长',
          label: '不长'
        }, {
          value: '苗弱',
          label: '苗弱'
        }, {
          value: '异常',
          label: '异常'
        }, {
          value: '其他',
          label: '其他'
      }],
      tsqds: [{
          value: '客服回访',
          label: '客服回访'
        }, {
          value: '抖音群',
          label: '抖音群'
        }, {
          value: '抖音',
          label: '抖音'
        }, {
          value: '客户投诉',
          label: '客户投诉'
        }, {
          value: '客户到店',
          label: '客户到店'
      }],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询SysTsWorkOrderBi列表 */
    getList() {
      this.loading = true;
      listSysTsWorkOrderBi(this.queryParams).then(response => {
        this.SysTsWorkOrderBiList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        year: null,
        wkYear: null,
        wkDay: null,
        wtfl: null,
        tsqd: null,
        area: null,
        cus: null,
        cphone: null,
        saleman: null,
        sphone: null,
        tscp: null,
        tswt: null,
        cljg: null,
        attach: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.year)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      const now = new Date();
      const year = now.getFullYear();
      const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始，所以加 1
      const day = String(now.getDate()).padStart(2, '0');
      let currentDate = year+"-"+month+"-"+day;
      this.form.year = String(year);
      this.form.wkYear = currentDate;
      this.open = true;
      this.title = "填写投诉工单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getSysTsWorkOrderBi(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改投诉工单";
      });
    },
    handleView(row){
      this.form1 = row;
      this.title1 = "投诉工单详情";
      this.open1 = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSysTsWorkOrderBi(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSysTsWorkOrderBi(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const years = row.year || this.ids;
      this.$modal.confirm('是否确认删除SysTsWorkOrderBi编号为"' + years + '"的数据项？').then(function() {
        return delSysTsWorkOrderBi(years);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/SysTsWorkOrderBi/export', {
        ...this.queryParams
      }, `投诉工单数据_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
